#!/usr/local/bin/php -f

<?php

require_once("config.inc");
require_once("functions.inc");
    
mwexec("/usr/local/sbin/squid -k rotate");
sleep(3);

$clients = get_current_client();

$db = sqlite_open('/var/db/web_access.db');

$result = sqlite_query($db, "SELECT name FROM sqlite_master WHERE type = 'table'" .
                       " AND name = 'web_access_stat'");

$count = intval(sqlite_fetch_single($result));
if ($count <= 0) {
  $result = sqlite_exec($db, "CREATE TABLE web_access_stat (
                              date   DATE,
                              hour   INT(11),
                              ip     CHAR(15),
                              name   CHAR(30),
                              domain CHAR(100),
                              full_domain CHAR(100),
                              size   INT(11),
                              req_count  INT(11),
                              text_count  INT(11),
                              PRIMARY KEY(date, hour, ip, full_domain))");
}


$lines = 0;
$uniq_rec = 0;
$up_rec = 0;
$access_map = array();
$domain_map = unserialize(file_get_contents('/tmp/domain_parser.cache'));
if(!is_array($domain_map))
{
    $domain_map = array();
}
if ($fh = fopen('/var/squid/log/access.log.0','r')) {
  while (! feof($fh)) {
    if ($line = fgets($fh)) {
      $fields = split(' +', $line);
      $htime = date("Y-m-d", trim($fields[0]));
      $hour = date("G", trim($fields[0]));
      $ip = trim($fields[2]);
      $size = trim($fields[4]);
      $url = parse_url(trim($fields[6]));
      $fdn = $url["host"];
      if(!isset($domain_map[$fdn]))
      {
        if(preg_match("/[^\.]+\.(com|cn|net|gov|org|edu)(\.cn)?$/", $fdn, $matches) > 0)
        {
            $domain_map[$fdn] = $matches[0];
        }
        else
        {
            $domain_map[$fdn] = $fdn;
        }
      }
      $dn = $domain_map[$fdn];
      
      $istext = 0;
      if (trim($fields[9]) == "text/html") {
        $istext = 1;
      }
      
      if(empty($dn) || $ip == '127.0.0.1')
      {
      	continue;
      }
      $data = array($htime, $hour, $ip, $dn, $fdn, $size, 1, $istext, $clients[$ip]['hostname']);
      $mul_key = $htime.$hour.$ip.$fdn;
      if (isset($access_map[$mul_key])) {
        $access_map[$mul_key][5] += $size;
        $access_map[$mul_key][6] += 1; 
        $access_map[$mul_key][7] += $istext; 
      }
      else {
        $access_map[$mul_key] = $data;
      }
      $lines++;
    }
  }
}


foreach ($access_map as $key => $value) {  
  $sql = "update web_access_stat set size=size+{$value[5]}, req_count=req_count+{$value[6]}, text_count=text_count+{$value[7]}";
  if(!empty($value[8]))
  {
    $sql .= ",name='{$value[8]}'";
  }
  $sql .= " where date='{$value[0]}' and hour={$value[1]} and ip='{$value[2]}' and full_domain='{$value[4]}'";
  $result = sqlite_exec($db, $sql);
  $exist = sqlite_changes($db);

  if (!$exist) {
    $sql = "insert into web_access_stat values('{$value[0]}','{$value[1]}','{$value[2]}','{$value[8]}','{$value[3]}','{$value[4]}','{$value[5]}','{$value[6]}','{$value[7]}')";
    $result = sqlite_exec($db, $sql);
    $inserted = sqlite_changes($db);
    if ($inserted) {
      $uniq_rec++;
    }
  }
  else {
    $up_rec++;
  }
}

unlink("/var/squid/log/access.log.0");

printf("%d %d %d\n", $lines, $uniq_rec, $up_rec);

$log_rotate = trim(`grep log_rotate /usr/local/stairway/conf/config.xml | sed -e 's/<[^>]*>//g'`);
if($log_rotate > 30)
    $log_rotate = 30;
$del_date = date('Y-m-d', time() - $log_rotate * 24 * 3600);
$result = sqlite_exec($db, "delete from web_access_stat where date < '$del_date'");
sqlite_close($db); 

if(count($domain_map) < 2000)
{
    file_put_contents('/tmp/domain_parser.cache', serialize($domain_map));
}
else
{
    unlink('/tmp/domain_parser.cache');
}

?>

